package com.zzh.myapplication.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.icarplus.car.R;
import net.icarplus.car.adapter.BookCarListAdapter;
import net.icarplus.car.adapter.CarListAdapter;
import net.icarplus.car.bean.BookCarListBean;
import net.icarplus.car.bean.CarImageBean;
import net.icarplus.car.bean.CarInfo;
import net.icarplus.car.bean.CarListBean;
import net.icarplus.car.bean.ResultInfo;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.ToolBarHelper;
import net.icarplus.car.views.WaterDropListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/10 11:51
 * 修改人：Gophy
 * 修改时间：16/3/10 11:51
 * 修改备注：
 */
public class DayRentCarActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, WaterDropListView.IWaterDropListViewListener, AdapterView.OnItemClickListener {

    private Toolbar toolbar;
    private RadioGroup rbSelectCar;
    private RadioButton rbNow;

    private WaterDropListView lvCars;
    private WaterDropListView waterDropListView;
    private CarListAdapter adapter;
    private ArrayList<CarListBean> lists = new ArrayList<CarListBean>();
    private CarListBean model;
    private LoadingUtil loadingUtil;
    private JSONArray array;
    private JSONObject jsons;

    private String hourRent = "01"; //时租
    private String bookDayRent = "05"; //预约日租
    private boolean isRefresh = false;
    private RadioGroup rgTabSelect;
    private BookCarListAdapter bookAdapter;
    private ArrayList<BookCarListBean> bookLists = new ArrayList<BookCarListBean>();
    private BookCarListBean bookModel;

    private String type = "02";

    private int pageNum = 1;

    private boolean isLater = false;
    private boolean isNow = false;

    private List<CarInfo> savaCarInfo = new ArrayList<CarInfo>();


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    lvCars.stopRefresh();
                    break;
                case 2:
                    lvCars.stopLoadMore();
                    break;
                case 1000:
                    DayRentCarActivity.this.loadingUtil.stopShowLoading();
                    Log.e("1000", msg.obj.toString());
                    refreshCarList(msg.obj.toString());


                    break;
                case 3000:
                    Log.e("3000", "3000000");
                    break;
                default:
                    break;
            }

        }
    };


    @Override
    public void setContent() {
        setContentView(R.layout.activity_day_rent_car);
    }

    @Override
    public void initComponent() {
        this.loadingUtil = new LoadingUtil(this, "查询中...");
        initView();
//        if (!isLater) {
//            setCarListsParams();
//        }
    }

    @Override
    public void setListener() {
        lvCars.setWaterDropListViewListener(this);
        lvCars.setPullLoadEnable(true);
        waterDropListView.setWaterDropListViewListener(this);
        waterDropListView.setPullLoadEnable(true);
        waterDropListView.setOnItemClickListener(this);
    }

    @Override
    public void onAfterInit() {

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        rbSelectCar = (RadioGroup) findViewById(R.id.tab_select_car);
        rbSelectCar.setOnCheckedChangeListener(this);
        rbNow = (RadioButton) findViewById(R.id.rb_use_now);
        lvCars = (WaterDropListView) findViewById(R.id.lv_car_list);
        waterDropListView = (WaterDropListView) findViewById(R.id.lv_car_list);
        rbNow.setChecked(true);
        ToolBarHelper.setBack(this, toolbar);
        this.loadingUtil.startShowLoading();
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        switch (checkId) {
            case R.id.rb_use_now:

                if (isNow && lists.size() > 0) {
                    lists.clear();
                }
                isLater = false;
                type = "02";
                pageNum = 1;
//                setCarListsParams(type, pageNum);
                this.loadingUtil.startShowLoading();
                PublicRequest.queryNearbyCars(this,handler,pageNum,"",type);
                break;
            case R.id.rb_use_later:
                if (bookLists.size() > 0) {
                    bookLists.clear();
                }
                isNow = true;
                isLater = true;
                pageNum = 1;
                type = "0201";
                this.loadingUtil.startShowLoading();
                PublicRequest.queryNearbyCars(this, handler, pageNum, "",type);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh() {
        if (!isLater) {
            if (lists.size() > 0) {
                lists.clear();
            }
        } else {
            if (bookLists.size() > 0) {
                bookLists.clear();
            }
        }
        pageNum = 1;
//        setCarListsParams(type, pageNum);
        this.loadingUtil.startShowLoading();
        PublicRequest.queryNearbyCars(this, handler, pageNum, "",type);
        handler.sendEmptyMessage(1);


    }

    @Override
    public void onLoadMore() {
        pageNum++;
//        setCarListsParams(type, pageNum);
        this.loadingUtil.startShowLoading();
        PublicRequest.queryNearbyCars(this, handler, pageNum, "",type);
        handler.sendEmptyMessage(2);


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CarInfo carInfo = null;
        for (int i = 0; savaCarInfo != null && i < savaCarInfo.size(); i++) {
            carInfo = savaCarInfo.get(position - 1);
            List<CarImageBean> carImage = carInfo.carImgList;
            for (int j = 0; carImage != null && j < carImage.size(); j++) {
                CarImageBean carImageBean = carImage.get(j);
                Intent intent = new Intent(DayRentCarActivity.this, DayRenCarListDetailActicy.class);
                intent.putExtra("title", carInfo.modelName);
                intent.putExtra("carNum", carInfo.licenseNo);
                intent.putExtra("picUrl", UrlValues.CAR_IMG_ID + carImageBean.imgId);
                intent.putExtra("carNum",carInfo.licenseNo);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                start_Activity(intent);
            }
        }
    }

    private void refreshCarList(String carList) {
        if (carList != null) {

            Gson gson = new Gson();
            ResultInfo result = gson.fromJson(carList,
                    new TypeToken<ResultInfo>() {
                    }.getType());

            savaCarInfo = result.queryList;

            if (!isLater) {
                adapter = new CarListAdapter(DayRentCarActivity.this, savaCarInfo);
                adapter.notifyDataSetChanged();
                waterDropListView.setAdapter(adapter);
            }else {
                bookAdapter = new BookCarListAdapter(DayRentCarActivity.this, savaCarInfo);
                bookAdapter.notifyDataSetChanged();
                waterDropListView.setAdapter(bookAdapter);

            }

        }
    }
}

